package com.qctpos.crm.webapi.config;


import lombok.Data;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author xxj
 * @description
 */
@Data
@Configuration(value = "rabbitConfig")
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    /**
     * 消息队列名
     */
    @Value("${properties.queue.logback}")
    private String queueLog;


    /**
     * 交换空间名称
     */
    @Value("${properties.queue.logbackexchange}")
    public String EXCHANGE_LOGBACK;

    /**
     * 设置路由key
     */
    @Value("${properties.queue.logbackrouting}")
    public String ROUTINGKEY_LOGBACK;



    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        return connectionFactory;
    }


    @Bean
    public DirectExchange logExchange() { // 使用直连的模式
        return new DirectExchange(EXCHANGE_LOGBACK, true, false);
    }


    @Bean
    public Binding bindingQueueLogAdd() {
        return BindingBuilder.bind(queueLog()).to(logExchange()).with(ROUTINGKEY_LOGBACK);
    }


    /**
     * 将需要创建的消息队列实例化
     */
    @Bean
    public Queue queueLog() {
        return new Queue(queueLog, true);
    }


}
